//
// Created by whg on 2025-06-17.
//

#include "frameObject.hpp"

FrameObject::FrameObject(CodeObject *code) {
    init(code);
    _globals = _locals;
}

FrameObject::FrameObject(FunctionObject* func, ObjList args) {
    init(func->_funcCode);
    _globals = func->_globals;

    _fastLocals = new ArrayList<PyObject*>();
    if(func->_defaults){
        int dftCnt = func->_defaults->length();
        int argCnt = func->_funcCode->_argCount;
        while(dftCnt--){
            _fastLocals->set(--argCnt, func->_defaults->get(dftCnt));
        }
    }
    if(args){
        for(int i=0;i<args->length();i++){
            _fastLocals->set(i, args->get(i));
        }
    }
}

void FrameObject::init(CodeObject *code) {
    _code = code;
    _pc = 0;

    _stack = new ArrayList<PyObject*>(code->_stackSize);
    _consts = code->_consts;
    _names = code->_names;
    _locals = new Map<PyObject*, PyObject*>();
}

unsigned char FrameObject::getOpCode() {
    return _code->_byteCodes->value()[_pc++];
}

int FrameObject::getOpArg() {
    return _code->_byteCodes->value()[_pc++] & 0xff;
}

bool FrameObject::hasMoreCode() {
    return _pc < _code->_byteCodes->length();
}
